package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigchun.woodhunter.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import bean.ContentDataBean;
import bean.ShowRoomAllDataBean;
import myapp.MyApp;
import util.GlideUtils;
import util.OrderEventMessage;

/**
 * Created by fei on 2017/12/30.
 */

public class ListviewAdapter extends BaseAdapter {
    private List<ShowRoomAllDataBean> mDatas = new ArrayList<>();
    private Context context;
    private LayoutInflater mInflater;
    private ContentDataBean.DataBean beantran;
    public ListviewAdapter(List<ShowRoomAllDataBean> mDatas, Context context, MyApp myApp, ContentDataBean.DataBean beantran) {
        this.mDatas = mDatas;
        this.context = context;
        this.beantran = beantran;
        this.mInflater = LayoutInflater.from(context);
//        this.myApp = myApp;

    }
    public void setData(List<ShowRoomAllDataBean> mDatas){
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {

            holder=new ViewHolder();

            convertView = mInflater.inflate(R.layout.item_showroom, null);

            holder.tv_zhanting_name = convertView.findViewById(R.id.tv_zhanting_name);
            holder.tv_content = convertView.findViewById(R.id.tv_content);
            holder.tv_phone = convertView.findViewById(R.id.tv_phone);
            holder.tv_time = convertView.findViewById(R.id.tv_time);
            holder.tv_location = convertView.findViewById(R.id.tv_location);
            holder.img = convertView.findViewById(R.id.img);
            holder.ll_zhantinggoods = convertView.findViewById(R.id.ll_zhantinggoods);
            holder.ll_location = convertView.findViewById(R.id.ll_location);
            holder.tv_shou_phone = convertView.findViewById(R.id.tv_shou_phone);
            holder.tv_opentime = convertView.findViewById(R.id.tv_opentime);
            holder.tv_goods_link = convertView.findViewById(R.id.tv_goods_link);

            convertView.setTag(holder);

        }else {
            holder = (ViewHolder)convertView.getTag();
        }


        //holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
        // MyLog.i("", msg);

        if (beantran != null){
            holder.tv_shou_phone.setText(beantran.getSale_phone());
            holder.tv_opentime.setText(beantran.getBusiness_hours());
            holder.tv_goods_link.setText(beantran.getRecome_good_link());
        }

        holder.tv_zhanting_name.setText(mDatas.get(i).getName());
        holder.tv_phone.setText(mDatas.get(i).getPhone());
        holder.tv_time.setText(mDatas.get(i).getStart_time()+"-"+mDatas.get(i).getEnd_time());
        holder.tv_location.setText(mDatas.get(i).getLocation());
//                    tv_content.setText(contentStr);

        holder.tv_content.getSettings().setDefaultTextEncodingName("UTF-8");
        holder.tv_content.loadDataWithBaseURL(null, mDatas.get(i).getContent()+"<style> img{max-width:100%;max-height:100%} </style>", "text/html", "utf-8", null);
        holder.tv_content.setWebViewClient(new WebViewClient());

        if (!"".equals(mDatas.get(i).getImage())){
            holder.img.setVisibility(View.VISIBLE);
            GlideUtils.loadRoundCircleImage(context,mDatas.get(i).getImage(),holder.img);
        }else{
            holder.img.setVisibility(View.GONE);
        }
        final String ztname = mDatas.get(i).getName();
        final String ztlocation = mDatas.get(i).getLocation();
        holder.ll_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new OrderEventMessage("跳转地图导航",ztlocation));
            }
        });

        holder.ll_zhantinggoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new OrderEventMessage("跳转商品链接",ztname));
            }
        });

        return convertView;
    }

    public final class ViewHolder{
        private TextView tv_zhanting_name;
        private WebView tv_content;
        private TextView tv_phone;
        private TextView tv_time;
        private TextView tv_location;
        private ImageView img;
        private LinearLayout ll_zhantinggoods;
        private LinearLayout ll_location;
        private TextView tv_shou_phone;
        private TextView tv_opentime;
        private TextView tv_goods_link;
    }

}